package com.tjbank.cssys.web.controller;


import com.tjbank.cssys.domain.CkFeedbackTable;
import com.tjbank.cssys.domain.CkLog;
import com.tjbank.cssys.web.service.CkFeedBackService;
import com.tjbank.cssys.web.service.CkLogService;
import com.tjbank.cssys.web.util.CommonResult;
import com.tjbank.cssys.web.util.GetUserCodeFromToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/feedBack")
@Api(tags = "CkFeedBackController", description = "反馈码管理")
public class CkFeedBackController {
    @Autowired
    private CkFeedBackService ckFeedBackService;
    @Autowired
    private CkLogService ckLogService;

    private static  final Logger logger = LoggerFactory.getLogger(CkFeedBackController.class);

    /**
     * 添加数据
     * @param ckFeedbackTable
     * @return
     */
    @PutMapping("/add")
    private CommonResult addFeedBack(@RequestBody CkFeedbackTable ckFeedbackTable, HttpServletRequest request) {
        ckFeedbackTable.setState(1);
        ckFeedBackService.insert(ckFeedbackTable);

        String userCode=null;
        try {
            userCode= GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {
            logger.error("获取当前登录人信息异常"+e.getMessage());
        }
        CkLog ckLog = new CkLog();
        ckLog.setLogId(UUID.randomUUID().toString());
        ckLog.setLogName(UUID.randomUUID().toString());
        ckLog.setOperationType("添加数据");
        ckLog.setUserId(userCode);
        Date date = new Date();
        ckLog.setTimes(new Date(date.getTime()));
        ckLog.setParams(ckFeedbackTable+"");
        ckLog.setResult("success:200");
        ckLogService.addInsert(ckLog);
        return CommonResult.success("");
    }

    /**
     * 根据id更新
     * @param ckFeedbackTable
     * @return
     */
    @PutMapping("/update")
    private CommonResult updateFeedBack(@RequestBody CkFeedbackTable ckFeedbackTable,HttpServletRequest request) {
        ckFeedBackService.updateByPrimaryKey(ckFeedbackTable);


        String userCode=null;
        try {
            userCode= GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {
            logger.error("获取当前登录人信息异常"+e.getMessage());
        }
        CkLog ckLog = new CkLog();
        ckLog.setLogId(UUID.randomUUID().toString());
        ckLog.setLogName(UUID.randomUUID().toString());
        ckLog.setOperationType("根据id更新");
        ckLog.setUserId(userCode);
        Date date = new Date();
        ckLog.setTimes(new Date(date.getTime()));
        ckLog.setParams(ckFeedbackTable+"");
        ckLog.setResult("success:200");
        ckLogService.addInsert(ckLog);
        return CommonResult.success("");
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    private CommonResult deleteFeedBack(@RequestParam("id") Long id , HttpServletRequest request) {
        ckFeedBackService.deleteByPrimaryKey(id);

        String userCode=null;
        try {
            userCode= GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {
            logger.error("获取当前登录人信息异常"+e.getMessage());
        }
        CkLog ckLog = new CkLog();
        ckLog.setLogId(UUID.randomUUID().toString());
        ckLog.setLogName(UUID.randomUUID().toString());
        ckLog.setOperationType("根据id删除");
        ckLog.setUserId(userCode);
        Date date = new Date();
        ckLog.setTimes(new Date(date.getTime()));
        ckLog.setParams(id+"");
        ckLog.setResult("success:200");
        ckLogService.addInsert(ckLog);

        return CommonResult.success("");
    }

    /**
     * 查询
     * @return
     */
    @GetMapping("/selectList")
    private CommonResult selectList(HttpServletRequest request) {
        List<CkFeedbackTable> ckFeedbackTables = ckFeedBackService.selectList();

        String userCode=null;
        try {
            userCode= GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {
            logger.error("获取当前登录人信息异常"+e.getMessage());
        }
        CkLog ckLog = new CkLog();
        ckLog.setLogId(UUID.randomUUID().toString());
        ckLog.setLogName(UUID.randomUUID().toString());
        ckLog.setOperationType("查询");
        ckLog.setUserId(userCode);
        Date date = new Date();
        ckLog.setTimes(new Date(date.getTime()));
        ckLog.setParams("");

        if(CollectionUtils.isNotEmpty(ckFeedbackTables)){
            ckLog.setResult("success:200");
            ckLogService.addInsert(ckLog);
            return CommonResult.success(ckFeedbackTables);
        }else{
            ckLog.setResult("error:500");
            ckLogService.addInsert(ckLog);
            return CommonResult.failed("操作错误");
        }
    }


    /**
     * 获取失联原因和停催原因 列表
     * @return data:{"tcFeedBack":[CkFeedbackTable,...],"slFeedBack":[CkFeedbackTable,...]}
     */
    @ApiOperation("获取失联原因和停催原因 列表")
    @GetMapping("/selectList/type")
    private CommonResult selectTcAndSlBack(HttpServletRequest request){
        Map<String, Object> dataMap = ckFeedBackService.selectCkFeedbackByType();

        String userCode=null;
        try {
            userCode= GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {
            logger.error("获取当前登录人信息异常"+e.getMessage());
        }
        CkLog ckLog = new CkLog();
        ckLog.setLogId(UUID.randomUUID().toString());
        ckLog.setLogName(UUID.randomUUID().toString());
        ckLog.setOperationType("获取失联原因和停催原因查询");
        ckLog.setUserId(userCode);
        Date date = new Date();
        ckLog.setTimes(new Date(date.getTime()));
        ckLog.setParams("");
        ckLog.setResult("success:200");
        ckLogService.addInsert(ckLog);

        return CommonResult.success(dataMap,"查询成功");
    }

}
